//
//  XZMyExamCell.m
//  Teecloud
//
//  Created by PC on 2018/10/25.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZMyExamCell.h"
#import "XZExamModel.h"
#import "ZJTimeCountDown.h"
@interface XZMyExamCell ()
@property (nonatomic, strong) ZJTimeCountDown *countDown;
//图片
@property (weak, nonatomic) IBOutlet UIImageView *headViewImg;
//名称
@property (weak, nonatomic) IBOutlet UILabel *titleNameLabel;
//来源
@property (weak, nonatomic) IBOutlet UILabel *fromNameLabel;
//状态 倒计时 未考试等
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;

@end
//通过
#define KPASSColor [UIColor colorWithHexString:@"#009900" andAlpha:1.0]
//未通过
#define KUNPASSColor [UIColor colorWithHexString:@"#FF0000" andAlpha:1.0]
//培训中
#define KTrainColor [UIColor colorWithHexString:@"#FF9900" andAlpha:1.0]
@implementation XZMyExamCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setModel:(XZExamModel *)model{
    _model = model;
     [self.headViewImg yy_setImageWithURL:[NSURL URLWithString:model.HeadUrl] placeholder:DefealtImg];
    self.titleNameLabel.text  = model.Name;
    self.fromNameLabel.text = model.FromName;
    
    if ([model.TestStatus integerValue] == 0) {
        NSInteger second = [model.SpaceTime integerValue];
        [self.countDown zj_timeCountDownWithSecondTime:second completeBlock:^(NSInteger hour, NSInteger minute, NSInteger second) {
            self.statusLabel.text = [NSString stringWithFormat:@"距离开考 %02ld:%02ld:%02ld",(long)hour,(long)minute,(long)second];
        }];
    }else if ([model.TestStatus integerValue] == 1){
        self.statusLabel.textColor =[UIColor colorWithHexString:@"#0065EA" andAlpha:1.0f];
        self.statusLabel.text = @"进入考试";
    }else if ([model.TestStatus integerValue] == 2){
        self.statusLabel.textColor =[UIColor colorWithHexString:@"#0065EA" andAlpha:1.0f];
        self.statusLabel.text = @"继续考试";
    }else if ([model.TestStatus integerValue] == 3){
        self.statusLabel.text = @"停止入场";
    }else if ([model.TestStatus integerValue] == 9){
        if ([model.PassMode integerValue] == -1) { //缺考
            self.statusLabel.text = @"缺考";
            self.statusLabel.textColor = kRedColor;
        }else if ([model.PassMode integerValue] == 0){//未通过
            NSString *passString = [NSString stringWithFormat:@"%@分(未通过)",model.Score];
            NSString *scoreStr = [NSString stringWithFormat:@"%@",model.Score];
            [self textAttributedString:passString scoreString:scoreStr label:self.statusLabel];
        }else if ([model.PassMode integerValue] == 1){//通过
            NSString *passString = [NSString stringWithFormat:@"%@分(通过)",model.Score];
            NSString *scoreStr = [NSString stringWithFormat:@"%@",model.Score];
            [self textAttributedString:passString scoreString:scoreStr label:self.statusLabel];
        }
    
    }
    
}


- (void)textAttributedString:(NSString *)string scoreString:(NSString *)scoreString label:(UILabel *)label{
    
    NSMutableAttributedString *originalAttributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [originalAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
    
    [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KTrainColor range:NSMakeRange(0, scoreString.length)];
    
    //字符串末尾有(未通过)
    if ([string hasSuffix:@"(未通过)"]) {
        [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KUNPASSColor range:NSMakeRange(string.length-5, 5)];
        
    } else {
        [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KPASSColor range:NSMakeRange(string.length-4, 4)];
    }
    label.attributedText = originalAttributedString;
}
#pragma mark - Setter
- (ZJTimeCountDown *)countDown{
    if (!_countDown) {
        _countDown = [[ZJTimeCountDown alloc]init];
    }
    return _countDown;
}
//销毁定时器
- (void)dealloc{
    [self.countDown zj_timeDestoryTimer];
}
@end
